package com.kobeliu.controller;

import com.kobeliu.api.thread.MyThreadPool;
import com.kobeliu.pojo.DataDictionary;
import com.kobeliu.pojo.bo.DataDictionaryBO;
import com.kobeliu.pojo.bo.QueryDictItemsBO;
import com.kobeliu.pojo.vo.CompanyPointsVO;
import com.kobeliu.result.GraceJSONResult;
import com.kobeliu.result.ResponseStatusEnum;
import com.kobeliu.service.DataDictService;
import com.kobeliu.utils.PagedGridResult;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

@RestController
@RequestMapping("dataDict")
@Slf4j
public class DataDictionaryController {

    @Autowired
    private DataDictService dataDictService;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    /**
     * 创建数据字典
     * @param dataDictionaryBO
     * @return
     */
    @PostMapping("create")
    public GraceJSONResult create(@RequestBody DataDictionaryBO dataDictionaryBO){


        dataDictService.createOrUpdateDataDict(dataDictionaryBO);

        return GraceJSONResult.ok();
    }

    /**
     * 分页查询数据字典
     * @param typeName
     * @param itemValue
     * @param page
     * @param limit
     * @return
     */
    @PostMapping("list")
    public GraceJSONResult create(String typeName,String itemValue,Integer page,Integer limit){

        if(page == null) page = 1;
        if(limit == null) limit =10;


        PagedGridResult listResult = dataDictService.dataDictList(typeName,itemValue,page,limit);

        return GraceJSONResult.ok(listResult);
    }

    /**
     * 创建数据字典
     * @param dataDictionaryBO
     * @return
     */
    @PostMapping("modify")
    public GraceJSONResult modify(@RequestBody DataDictionaryBO dataDictionaryBO){

        if(dataDictionaryBO.getId()==null) return GraceJSONResult.error();

        dataDictService.createOrUpdateDataDict(dataDictionaryBO);

        return GraceJSONResult.ok();
    }


//    @PostMapping("item/{dictId}")
    @PostMapping("item")
    public GraceJSONResult item(String dictId){

        return GraceJSONResult.ok(dataDictService.getDataDict(dictId));
    }

    @PostMapping("delete/{dictId}")
    public GraceJSONResult delete(@PathVariable("dictId")String dictId){

        dataDictService.delDataDict(dictId);

        return GraceJSONResult.ok();
    }

    // -================================APP使用接口==================

    /**
     * 根据传过来的key数组 去数据字典中拿取全部的value返回
     * @param itemsBO
     * @return
     */
    @PostMapping("app/getItemsByKeys1")
    public GraceJSONResult getItemsByKeys1(@RequestBody QueryDictItemsBO itemsBO){

        String[] advantages = itemsBO.getAdvantage();
        String[] bonus = itemsBO.getBonus();
        String[] subsidy = itemsBO.getSubsidy();
        String[] benefits = itemsBO.getBenefits();

        List<DataDictionary> advantageList = dataDictService.selectItemsByKeys(advantages);
        List<DataDictionary> bonusList = dataDictService.selectItemsByKeys(bonus);
        List<DataDictionary> subsidyList = dataDictService.selectItemsByKeys(subsidy);
        List<DataDictionary> benefitsList = dataDictService.selectItemsByKeys(benefits);

        CompanyPointsVO companyPointsVO = new CompanyPointsVO();
        companyPointsVO.setAdvantageList(advantageList);
        companyPointsVO.setBonusList(bonusList);
        companyPointsVO.setSubsidyList(subsidyList);
        companyPointsVO.setBenefitsList(benefitsList);

        return GraceJSONResult.ok(companyPointsVO);

    }

    //串行改并行
    @PostMapping("app/getItemsByKeys")
    public GraceJSONResult getItemsByKeys(@RequestBody QueryDictItemsBO itemsBO) throws ExecutionException, InterruptedException {

        CompanyPointsVO companyPointsVO = new CompanyPointsVO();

        CompletableFuture<Void> advantageFuture = CompletableFuture.runAsync(() -> {
            String[] advantages = itemsBO.getAdvantage();
            List<DataDictionary> advantageList = dataDictService.selectItemsByKeys(advantages);
            companyPointsVO.setAdvantageList(advantageList);
        },threadPoolExecutor);
        CompletableFuture<Void> benefitsFuture = CompletableFuture.runAsync(() -> {
            String[] benefits = itemsBO.getBenefits();
            List<DataDictionary> benefitsList = dataDictService.selectItemsByKeys(benefits);
            companyPointsVO.setBenefitsList(benefitsList);
        },threadPoolExecutor);
        CompletableFuture<Void> bonusFuture = CompletableFuture.runAsync(() -> {
            String[] bonus = itemsBO.getBonus();
            List<DataDictionary> bonusList = dataDictService.selectItemsByKeys(bonus);
            companyPointsVO.setBonusList(bonusList);
        },threadPoolExecutor);
        CompletableFuture<Void> subsidyFuture = CompletableFuture.runAsync(() -> {
            String[] subsidy = itemsBO.getSubsidy();
            List<DataDictionary> subsidyList = dataDictService.selectItemsByKeys(subsidy);
            companyPointsVO.setSubsidyList(subsidyList);
        },threadPoolExecutor);

        // 多重任务执行
        CompletableFuture future = CompletableFuture.allOf(
                advantageFuture,bonusFuture,benefitsFuture,subsidyFuture);

        //等待全部执行完毕
        future.get();

        return GraceJSONResult.ok(companyPointsVO);

    }

    /**
     * 根据key查询value
     * @param typeCode
     * @return
     */
    @PostMapping("/app/getDataByCode")
    public GraceJSONResult getDataByCode(String typeCode){

        return GraceJSONResult.ok(dataDictService.getDataByCode(typeCode));

    }


}
